import logging
from PySide6.QtCore import Signal, QObject, QThread


class QTextBrowserHandler(logging.Handler, QObject):
    """自定义Qt日志处理器"""

    signal = Signal(str)

    def __init__(self):
        """初始化"""
        logging.Handler.__init__(self)
        QObject.__init__(self)

    def emit(self, record):
        """重写日志输出方法"""
        msg = self.format(record)
        self.signal.emit(msg)


class QWorkThread(QThread):
    """自定义Qt线程"""

    def set_run(self, run):
        """设置线程执行函数"""

        def run_func():
            try:
                run()
            except Exception as e:
                logger = logging.getLogger("qt")
                logger.error(f"run函数执行时发生异常: {e}")

        self.run = run_func
